﻿Imports System.Data
Imports System.Data.SqlClient
Public Class KhuVucDAO
    Public Function GetAll() As DataTable
        Dim cnn As New SqlConnection
        Dim dataProvider As New DataProvider
        Dim da As SqlDataAdapter
        Dim strCon As String = "select *from KHUVUC"
        cnn = DataProvider.ConnectionData()
        da = New SqlDataAdapter(strCon, cnn)
        Dim data As New DataTable
        Try
            da.Fill(data)
        Catch ex As Exception

        End Try
        Return data
    End Function
    Public Sub Insert(ByVal kv As KhuVucDTO)
        Try
            Dim data As New DataProvider
            Dim cnn As New SqlConnection
            cnn = data.ConnectionData()
            Dim cmd As SqlCommand = New SqlCommand()
            cmd.Connection = cnn
            cmd.CommandText = "exec usp_InsertKHUVUC @TenKhuVuc,@MaKhuVuc"
            Dim para As SqlParameter = cmd.Parameters.Add("@TenKhuVuc", SqlDbType.NVarChar)
            para.Value = kv.TenKhuVuc
            para = cmd.Parameters.Add("@MaKhuVuc", SqlDbType.Int)
            para.Value = kv.MaKhuVuc
            cmd.ExecuteNonQuery()
            MessageBox.Show("Khu vực đã được tạo thành công", "Thông báo")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub Update(ByVal kv As KhuVucDTO)
        Try
            Dim data As New DataProvider
            Dim cnn As New SqlConnection
            cnn = data.ConnectionData()
            Dim cmd As SqlCommand = New SqlCommand()
            cmd.Connection = cnn
            cmd.CommandText = "exec usp_UpdateKHUVUC @TenKhuVuc,@MaKhuVuc"
            Dim para As SqlParameter = cmd.Parameters.Add("@TenKhuVuc", SqlDbType.NVarChar)
            para.Value = kv.TenKhuVuc
            para = cmd.Parameters.Add("@MaKhuVuc", SqlDbType.Int)
            para.Value = kv.MaKhuVuc
            cmd.ExecuteNonQuery()
            MessageBox.Show("Khu vực đã được cập nhật thành công", "Thông báo")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    Public Sub Delete(ByVal kv As KhuVucDTO)
        Try
            Dim data As New DataProvider
            Dim cnn As New SqlConnection
            cnn = data.ConnectionData()
            Dim cmd As SqlCommand = New SqlCommand()
            cmd.Connection = cnn
            cmd.CommandText = "exec usp_UpdateKHUVUC @MaKhuVuc"
            Dim para As SqlParameter = cmd.Parameters.Add("@MaKhuVuc", SqlDbType.Int)
            para.Value = kv.MaKhuVuc
            cmd.ExecuteNonQuery()
            MessageBox.Show("Khu vực đã được xóa thành công", "Thông báo")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class
